package com.itheima.controller.backend.Service.ServiceImpl;

import com.itheima.DTO.OrderSettingDTO;
import com.itheima.VO.OrderSettingVO;
import com.itheima.controller.backend.Service.OrderSettingService;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void editNumberByOrderDate(OrderSettingDTO orderSettingDTO){
        log.info("根据日期修改可预约人数：{}",orderSettingDTO);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        LocalDate orderDate = LocalDate.parse(df.format(orderSettingDTO.getOrderDate()));
        orderSettingMapper.editNumberByOrderDate(orderSettingDTO.getNumber(),orderDate);
    }


    @Override
    public List<OrderSettingVO> getOrderSettingByMonth(String month){

        if (month.length()==6){
            String[] split = month.split("-");
            split[1]="0"+split[1];
            month=split[0]+"-"+split[1];
        }

        LocalDate begin = LocalDate.parse(month + "-01");
        LocalDate end = begin.plusMonths(1).plusDays(-1);

        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(begin, end);
        List<OrderSettingVO> list1 = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            OrderSettingVO orderSettingVO = new OrderSettingVO();
            orderSettingVO.setDate(orderSetting.getOrderDate().getDate());
            orderSettingVO.setNumber(orderSetting.getNumber());
            orderSettingVO.setReservations(orderSetting.getReservations());
            list1.add(orderSettingVO);
        }
        return list1;
    }

    public void Insert(OrderSettingDTO orderSettingDTO){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

        // 解析日期时间字符串为 ZonedDateTime
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(orderSettingDTO.getOrderDate().toString(), formatter);

        // 转换为 LocalDate
        LocalDate orderDate = zonedDateTime.toLocalDate();

        orderSettingMapper.Insert(orderSettingDTO.getNumber(),orderDate);
    }

}
